package com.jt.www.biz.agree.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jt.www.biz.agree.service.AgreeManageService;
import com.jt.www.dao.mapper.agree.AgreeManageEntityMapper;
import com.jt.www.domain.enums.pc.AgreeStatusEnum;
import com.jt.www.domain.po.AgreeManageEntity;
import com.jt.www.domain.qo.AgreeManageQueryVO;
import com.jt.www.domain.vo.AddManageVO;
import com.jt.www.domain.vo.AgreeManageResultVO;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.reps.ResultDTO;
import com.jt.www.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.HashMap;
import java.util.List;

/**
 * @author admin_ltf
 * @create 2019-05-18 10:59
 */
@Service
public class AgreeManageServiceImpl implements AgreeManageService {

    @Autowired
    AgreeManageEntityMapper agreeManageEntityMapper;

    @Override
    public Integer addAgreeManageInfo(AgreeManageEntity agreeManageEntity) {
        if (agreeManageEntity == null) {
            return -1;
        }

        int num = agreeManageEntityMapper.insert(agreeManageEntity);

        return num;
    }

//    @Override
//    public Integer updateAgreeManageInfo(AgreeManageEntity agreeManageEntity) {
//        if(agreeManageEntity == null){
//            return -1;
//        }
//        int num =  agreeManageEntityMapper.updateByPrimaryKeySelective(agreeManageEntity);
//
//        return num;
//    }

    @Override
    public Integer updateAgree(AddManageVO addManageVO) {
        if (addManageVO == null) {
            return -1;
        }

        AgreeManageEntity agreeManageEntity = new AgreeManageEntity();

        agreeManageEntity.setAgreeStartTime(addManageVO.getAgreeStartTime());
        agreeManageEntity.setAgreeEndTime(addManageVO.getAgreeEndTime());
        agreeManageEntity.setStatus(AgreeStatusEnum.YES.getCode());
        agreeManageEntity.setUpdateTime(DateUtils.getCurrentTime());

        Weekend<AgreeManageEntity> weekend = Weekend.of(AgreeManageEntity.class);
        WeekendCriteria<AgreeManageEntity, Object> Criteria = weekend.weekendCriteria();
        Criteria.andEqualTo(AgreeManageEntity::getOrderNo, addManageVO.getOrderNo());
        int num = agreeManageEntityMapper.updateByExampleSelective(agreeManageEntity, weekend);

        return num;
    }

    @Override
    public Integer deleteAgree(String orderNo) {
        AgreeManageEntity agreeManageEntity = new AgreeManageEntity();
        agreeManageEntity.setStatus(AgreeStatusEnum.NO.getCode());
        agreeManageEntity.setUpdateTime(DateUtils.getCurrentTime());

        Weekend<AgreeManageEntity> weekend = Weekend.of(AgreeManageEntity.class);
        WeekendCriteria<AgreeManageEntity, Object> Criteria = weekend.weekendCriteria();
        Criteria.andEqualTo(AgreeManageEntity::getOrderNo, orderNo);

        int num = agreeManageEntityMapper.updateByExampleSelective(agreeManageEntity, weekend);
        return num;
    }

//    @Override
//    public Integer deleteAgreeManageInfo(String id) {
//        AgreeManageEntity agreeManageEntity = new AgreeManageEntity();
//        agreeManageEntity.setId(Long.valueOf(id));
//        agreeManageEntity.setIsDeleted(true);
//        agreeManageEntity.setUpdateTime(DateUtils.getCurrentTime());
//        int num =  agreeManageEntityMapper.updateByPrimaryKeySelective(agreeManageEntity);
//        return num;
//    }

    @Override
    public ResultDTO searchAgreeManageInfoByExample(AgreeManageQueryVO agreeManageQueryVO) {

        if (agreeManageQueryVO.getPage() != null && agreeManageQueryVO.getRows() != null) {
            PageHelper.startPage(agreeManageQueryVO.getPage(), agreeManageQueryVO.getRows());
        }


        // 第一步 先查订单 再查 list
        List<AgreeManageResultVO> resultList = agreeManageEntityMapper.queryOrderNo(agreeManageQueryVO);

        // 循环list 组装list
        resultList = createResultList(resultList);

//        List<AgreeManageEntity> resultList = agreeManageEntityMapper.queryByExample(agreeManageQueryVO);
        PageInfo pageinfo = new PageInfo(resultList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageCount", pageinfo.getTotal());
        map.put("result", resultList);

        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    private List<AgreeManageResultVO> createResultList(List<AgreeManageResultVO> agreeManageResultVOS) {

        for (AgreeManageResultVO vo : agreeManageResultVOS) {
            String orderNo = vo.getOrderNo();
            // 根据订单号查询
            AgreeManageEntity agreeManageEntity = new AgreeManageEntity();
            agreeManageEntity.setOrderNo(orderNo);
            List<AgreeManageEntity> list = agreeManageEntityMapper.select(agreeManageEntity);
            vo.setEntityList(list);
        }
        return agreeManageResultVOS;
    }


    @Override
    public ResultDTO searchInfoPageByExample(AgreeManageQueryVO agreeManageQueryVO) {

        if (agreeManageQueryVO.getPage() != null && agreeManageQueryVO.getRows() != null) {
            PageHelper.startPage(agreeManageQueryVO.getPage(), agreeManageQueryVO.getRows());
        }

        // 组装查询条件
        Weekend<AgreeManageEntity> weekend = Weekend.of(AgreeManageEntity.class);
        WeekendCriteria<AgreeManageEntity, Object> criteria = weekend.weekendCriteria();

        if (agreeManageQueryVO.getUserName() != null && !"".equals(agreeManageQueryVO.getUserName())) {
//            criteria.andEqualTo(AgreeManageEntity::getUserName,agreeManageQueryVO.getUserName());
            criteria.andLike(AgreeManageEntity::getUserName, "%" + agreeManageQueryVO.getUserName() + "%");
        }

        if (agreeManageQueryVO.getOrgCertificateCode() != null && !"".equals(agreeManageQueryVO.getOrgCertificateCode())) {
            criteria.andEqualTo(AgreeManageEntity::getOrgCertificateCode, agreeManageQueryVO.getOrgCertificateCode());
        }

        if (agreeManageQueryVO.getOrderNo() != null && !"".equals(agreeManageQueryVO.getOrderNo())) {
            criteria.andEqualTo(AgreeManageEntity::getOrderNo, agreeManageQueryVO.getOrderNo());
        }

        if (agreeManageQueryVO.getInsNo() != null && !"".equals(agreeManageQueryVO.getInsNo())) {
            criteria.andEqualTo(AgreeManageEntity::getInsNo, agreeManageQueryVO.getInsNo());
        }

        if (agreeManageQueryVO.getStatus() != null && !"".equals(agreeManageQueryVO.getStatus())) {
            criteria.andEqualTo(AgreeManageEntity::getStatus, agreeManageQueryVO.getStatus());
        }

        if (agreeManageQueryVO.getProCode() != null && !"".equals(agreeManageQueryVO.getProCode())) {
            criteria.andEqualTo(AgreeManageEntity::getProCode, agreeManageQueryVO.getProCode());
        }

        if (agreeManageQueryVO.getCityCode() != null && !"".equals(agreeManageQueryVO.getCityCode())) {
            criteria.andEqualTo(AgreeManageEntity::getCityCode, agreeManageQueryVO.getCityCode());
        }

        if (agreeManageQueryVO.getCountyCode() != null && !"".equals(agreeManageQueryVO.getCountyCode())) {
            criteria.andEqualTo(AgreeManageEntity::getCountyCode, agreeManageQueryVO.getCountyCode());
        }

        criteria.andEqualTo(AgreeManageEntity::getIsDeleted, false);
        weekend.setOrderByClause("create_time DESC");

//        List<AgreeManageEntity> resultList = agreeManageEntityMapper.queryByExample(agreeManageQueryVO);
        List<AgreeManageEntity> resultList = agreeManageEntityMapper.selectByExample(weekend);
        PageInfo pageinfo = new PageInfo(resultList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageCount", pageinfo.getTotal());
        map.put("result", resultList);

        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

//    @Override
//    public ResultDTO searchAgreeManageInfoByExample(AgreeManageQueryVO agreeManageQueryVO) {
//
//        if (agreeManageQueryVO.getPage() != null && agreeManageQueryVO.getRows() != null) {
//            PageHelper.startPage(agreeManageQueryVO.getPage(), agreeManageQueryVO.getRows());
//        }
//
//        // 组装查询条件
//        Weekend<AgreeManageEntity> weekend = Weekend.of(AgreeManageEntity.class);
//        WeekendCriteria<AgreeManageEntity, Object> criteria = weekend.weekendCriteria();
//
//        if(agreeManageQueryVO.getUserName() != null && !"".equals(agreeManageQueryVO.getUserName())){
//            criteria.andEqualTo(AgreeManageEntity::getUserName,agreeManageQueryVO.getUserName());
//        }
//
//        if(agreeManageQueryVO.getOrgCertificateCode() != null && !"".equals(agreeManageQueryVO.getOrgCertificateCode())){
//            criteria.andEqualTo(AgreeManageEntity::getOrgCertificateCode,agreeManageQueryVO.getOrgCertificateCode());
//        }
//
//        if(agreeManageQueryVO.getOrderNo() != null && !"".equals(agreeManageQueryVO.getOrderNo())){
//            criteria.andEqualTo(AgreeManageEntity::getOrderNo,agreeManageQueryVO.getOrderNo());
//        }
//
//        if(agreeManageQueryVO.getInsNo() != null && !"".equals(agreeManageQueryVO.getInsNo())){
//            criteria.andEqualTo(AgreeManageEntity::getInsNo,agreeManageQueryVO.getInsNo());
//        }
//
//        if(agreeManageQueryVO.getStatus() != null && !"".equals(agreeManageQueryVO.getStatus())){
//            criteria.andEqualTo(AgreeManageEntity::getStatus,agreeManageQueryVO.getStatus());
//        }
//
//        if(agreeManageQueryVO.getCountyCode() != null && !"".equals(agreeManageQueryVO.getCountyCode())){
//            criteria.andEqualTo(AgreeManageEntity::getCountyCode,agreeManageQueryVO.getCountyCode());
//        }
//
//        criteria.andEqualTo(AgreeManageEntity::getIsDeleted,false);
//        weekend.setOrderByClause("create_time DESC");
//
//
//
//        // 因为要根据订单分页先查询订单
//
//        //
//
//
//
//        List<AgreeManageEntity> resultList = agreeManageEntityMapper.selectByExample(weekend);
//        PageInfo pageinfo = new PageInfo(resultList);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("pageCount", pageinfo.getTotal());
//        map.put("result", resultList);
//
//        return new ResultDTO(ResultEnum.SUCCESS,map);
//    }

}
